using UnityEngine;
using System.Collections;

[AddComponentMenu("Third Person Enemies/Enemy Damage")]

public class EnemyDamage : MonoBehaviour
{
	public int hitPoints = 3;
	public Transform explosionPrefab;
	public Transform deadModelPrefab;
	public DroppableMover healthPrefab;
	public DroppableMover fuelPrefab;
	public int dropMin = 0;
	public int dropMax = 0;
	//Sound clips
	public AudioClip struckSound;
	
	private bool dead = false;
	
	void ApplyDamage(int damage)
	{
		//Play struck sound
		if(audio && struckSound)
		{
			audio.PlayOneShot(struckSound);
		}
		if(hitPoints <= 0)
		{
			return;
		}
		
		hitPoints -= damage;
		
		if(!dead && hitPoints <= 0)
		{
			Die();
			dead = true;
		}
	}
	
	void Die()
	{
		//Kill player
		Destroy(gameObject);
		
		//Instantiate replacement dead character model
		//*** check objectType
		Transform deadModel = (Transform)Instantiate(deadModelPrefab, transform.position, transform.rotation);
		CopyTransformsRecursive(transform, deadModel);
		
		//Create an effect to display enemy defeat
		Transform effect = (Transform)Instantiate(explosionPrefab, transform.position, transform.rotation);
		effect.parent = deadModel;
		
		//Enemy death effect
		//fall away and spin
		Rigidbody deadModelRigidbody = deadModel.rigidbody;
		Vector3 relativePlayerPosition = transform.InverseTransformPoint(Camera.main.transform.position);
		deadModelRigidbody.AddTorque(Vector3.up *7);
		
		if(relativePlayerPosition.z > 0)
		{
			deadModelRigidbody.AddForceAtPosition(-transform.forward * 2, transform.position + (transform.up * 5), ForceMode.Impulse);
		}
		else
		{
			deadModelRigidbody.AddForceAtPosition(transform.forward *2, transform.position + (transform.up * 2), ForceMode.Impulse);
		}
		
		//Drop a random number of pickups in a random fashion
		int toDrop = Random.Range(dropMin, dropMax +1);
		for(int i = 0; i < toDrop; i++)
		{
			//Pick a random directoin to throw pickup
			Vector3 direction = Random.onUnitSphere;
			if(direction.y < 0)
			{
				//Pickup cannot be thrown down
				direction.y = -direction.y;
			}
			
			Vector3 dropPosition = transform.TransformPoint(Vector3.up *1.5f) + (direction/2);
			
			DroppableMover dropped;
			
			//Select a pickup type at random
			if(Random.value > 0.5f)
			{
				dropped = (DroppableMover)Instantiate(healthPrefab, dropPosition, Quaternion.identity);
			}
			else
			{
				dropped =(DroppableMover)Instantiate(fuelPrefab, dropPosition, Quaternion.identity);
			}
			//set the pickup in motion
			dropped.Bounce((direction) * 4f *(float)(Random.value + 0.2));
		}
	}
		
		//deadModel
		// When the death sequence prefab is instantiated, all child elements
		//are copied over to clone by copying the transforms
		
		static void CopyTransformsRecursive(Transform src, Transform dst)
		{
			dst.position = src.position;
			dst.rotation = src.rotation;
			
			foreach(Transform child in dst)
			{
				//Match the transform with the same name
				Transform curSrc = src.Find(child.name);
				if(curSrc)
				{
					CopyTransformsRecursive(curSrc, child);
				}
			}
		}
}
